import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../controllers/fission_controller.dart';
import '../l10n/app_localizations.dart';

class InvitationHistory extends StatelessWidget {
  const InvitationHistory({super.key});

  @override
  Widget build(BuildContext context) {
    final FissionController controller = Get.find();
    final l10n = AppLocalizations.of(context);

    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
      decoration: BoxDecoration(
        // image: DecorationImage(
        //   image: AssetImage("assets/images/bg_area.png"),
        //   fit: BoxFit.fill,
        // ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // 标题
          Text(
            l10n.invitationHistory,
            style: TextStyle(
              color: Colors.white,
              fontSize: 16.sp,
              fontWeight: FontWeight.w600,
            ),
          ),

          SizedBox(height: 16.h),

          // 标签页
          Obx(
            () => Row(
              children:
                  controller.tabs.asMap().entries.map((entry) {
                    final index = entry.key;
                    final tab = entry.value;
                    final isSelected = index == controller.selectedTab.value;

                    return Expanded(
                      child: GestureDetector(
                        onTap: () => controller.selectTab(index),
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 2.w),
                          padding: EdgeInsets.symmetric(vertical: 8.h),
                          decoration: BoxDecoration(
                            color:
                                isSelected
                                    ? const Color(0xFF00FFFF).withOpacity(0.2)
                                    : Colors.transparent,
                            borderRadius: BorderRadius.circular(8.r),
                            border: Border.all(
                              color:
                                  isSelected
                                      ? const Color(0xFF00FFFF)
                                      : const Color(0xFF1A1F3A),
                              width: 1,
                            ),
                          ),
                          child: Center(
                            child: Text(
                              '$tab(${controller.currentTabCount})',
                              style: TextStyle(
                                color:
                                    isSelected
                                        ? const Color(0xFF00FFFF)
                                        : Colors.white70,
                                fontSize: 12.sp,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                          ),
                        ),
                      ),
                    );
                  }).toList(),
            ),
          ),

          SizedBox(height: 16.h),

          // 邀请历史列表
          Obx(() {
            final historyList = controller.currentTabHistory;

            // 如果列表为空，显示空数据提示
            if (historyList.isEmpty) {
              return Container(
                padding: EdgeInsets.symmetric(vertical: 60.h),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(
                      Icons.inbox_outlined,
                      size: 64.sp,
                      color: Colors.white.withOpacity(0.3),
                    ),
                    SizedBox(height: 16.h),
                    Text(
                      '暂无邀请记录',
                      style: TextStyle(
                        color: Colors.white.withOpacity(0.6),
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    SizedBox(height: 8.h),
                    Text(
                      '邀请好友一起来挖矿吧！',
                      style: TextStyle(
                        color: Colors.white.withOpacity(0.4),
                        fontSize: 12.sp,
                      ),
                    ),
                  ],
                ),
              );
            }

            // 列表不为空，显示数据
            return Column(
              children:
                  historyList.map((item) {
                    return Container(
                      margin: EdgeInsets.only(bottom: 12.h),
                      padding: EdgeInsets.all(16.w),
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage("assets/images/bg_selection.png"),
                          fit: BoxFit.fill,
                        ),
                      ),
                      child: Row(
                        children: [
                          // 用户信息和BTC数量
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Row(
                                children: [
                                  Image.asset(
                                    "assets/images/icon_avatar.png",
                                    width: 32.r,
                                    height: 32.r,
                                  ),
                                  SizedBox(width: 8.w),
                                  Text(
                                    item['user'],
                                    style: TextStyle(
                                      color: const Color(0xFF05FFFD),
                                      fontSize: 15.sp,
                                      fontWeight: FontWeight.w600,
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          ),

                          Expanded(
                            child: Column(
                              children: [
                                Text(
                                  '${item['btcAmount']} btc',
                                  style: TextStyle(
                                    color: const Color(0xFF00FFFF),
                                    fontSize: 12.sp,
                                  ),
                                ),
                                // Gh/s值
                                Text(
                                  '${item['ghs']} GH/S',
                                  style: TextStyle(
                                    color: const Color(0xFF00FFFF),
                                    fontSize: 12.sp,
                                  ),
                                ),
                              ],
                            ),
                          ),

                          SizedBox(height: 8.h),

                          // 我的佣金
                          Column(
                            children: [
                              Text(
                                l10n.myCommission,
                                style: TextStyle(
                                  color: Colors.white70,
                                  fontSize: 12.sp,
                                ),
                              ),
                              Text(
                                '${item['commission']}btc',
                                style: TextStyle(
                                  color: const Color(0xFFFFCC00),
                                  fontSize: 12.sp,
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    );
                  }).toList(),
            );
          }),
        ],
      ),
    );
  }
}
